home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: problems w/ relational operator ==
- Date: 17 Jan 1996 15:14:56 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4dj3pg$ldl@locutus.rchland.ibm.com>
- References: <30FC83D2.7B6A@hti.net>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <30FC83D2.7B6A@hti.net>, Michael Benrud <mbenrud@hti.net> writes:
- >I am having some problems using the == operator. I am using BC 4.02.
- >I have the following snipet of code:
- >
- >double i;
- >
- >if(i == 1.0)
- > Xinc = Xinc * 10;
- >
- >The problem is that when i equals 1.0, the if statement never executes
- >Xinc = Xinc * 10. Any ideas? Thanks in advance.
-
- Are you sure, really really sure, that i is *exactly* 1.0??? Remember,
- float and double are *approximations* and may not be exact. How is i
- calculated? Try this:
-
- if( i == 1.0 )
- Xinc *= 10;
- else
- cerr << "i not 1.0, diff is: " << ( i - 1.0 ) << endl;
-
- If you get some unbelievably small number in exponential notation then
- you know you have an inexact match. You're probably calculating i or
- incrementing it by some fraction that is not exactly representable.
- Then you could try:
-
- double maxerr = 0.00001;
-
- if( fabs( i - 1.0 ) < maxerr )
- Xinc *= 10;
-
- or something like that...
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-